﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using System.Collections;
using WXYW.DAL;

namespace WXYW
{
    public partial class SparePartApplyListNeedReturn : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSparePartApplyForm();
            }
        }

        private void BindSparePartApplyForm()
        {
            SparePartApplyService service = new SparePartApplyService();
            List<SparePartApplyForm> forms = new List<SparePartApplyForm>();
            if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.Administrator ||
                ContextService.CurrentAccount.UserType.ID == (int)RoleType.TBGLY ||
                ContextService.CurrentAccount.UserType.ID == (int)RoleType.SparePart)
            {
                forms = service.GetSparePartApplyByStatus("备件-待返还");
            }
            else
            {
                if (!ContextService.CurrentAccount.ServiceStationReference.IsLoaded)
                {
                    ContextService.CurrentAccount.ServiceStationReference.Load();
                }
                forms = service.GetSparePartApplyByApplicantAndStatus(ContextService.CurrentAccount.UserID, "备件-待返还");
            }
            RecordsNumber = forms.Count;
            sparePartApplyFormsGridView.DataSource = forms;
            sparePartApplyFormsGridView.DataBind();
        }

        protected void updateLinkButton_Click(object sender, EventArgs e)
        {
            string sparePartApplyID = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("SparePartApplyConfirmRetrunProduct.aspx?{0}={1}", Constants.QUERYSTR_SPAREPARTAPPLY_ID, sparePartApplyID));
        }

        protected void sparePartApplyFormsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < sparePartApplyFormsGridView.PageCount)
            {
                sparePartApplyFormsGridView.PageIndex = e.NewPageIndex;
                BindSparePartApplyForm();
            }
        }

        protected void sparePartApplyFormsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= sparePartApplyFormsGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (sparePartApplyFormsGridView.PageIndex + 1).ToString();
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<SparePartApplyForm> dds = new List<SparePartApplyForm>();
            dds.Add(new SparePartApplyForm());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            sparePartApplyFormsGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindSparePartApplyForm();
        }

        protected void sparePartApplyFormsGridView_DataBound(object sender, EventArgs e)
        {
            if (sparePartApplyFormsGridView.BottomPagerRow != null)
            {
                sparePartApplyFormsGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void rmalnk_Click(object sender, EventArgs e)
        {
            LinkButton lnkBtn = sender as LinkButton;
            string back_url = Request.Url.ToString();
            string url = string.Format("RMALinkDetail.aspx?APPLICATIOM_ID={0}&" + Constants.QUERYSTR_BACK_URL + "={1}",
                lnkBtn.CommandArgument, back_url);
            Response.Redirect(url);
        }
    }
}
